package org.watchrecords.managers;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.watchrecords.dbo.Offender;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wideplay.warp.persist.Transactional;
import com.wideplay.warp.persist.dao.Finder;

public class OffenderManager {
    private Provider<EntityManager> emProvider;
    
    protected OffenderManager() {}

    @Inject
    public OffenderManager(Provider<EntityManager> emProvider) {
        this.emProvider = emProvider;
    }
    
    public Offender getOffender(int id) {
        return emProvider.get().find(Offender.class, id);
    }
    
    @Transactional
    public void saveOffender(Offender offender) {
        offender.setUpdated(new Date());
        
        if (offender.getId() == null) {
            offender.setCreated(offender.getUpdated());
        }
        
        emProvider.get().persist(offender);
    }
    
    @Transactional
    public void deleteOffender(Offender offender) {
        emProvider.get().remove(emProvider.get().merge(offender));
    }
    
    @Finder(namedQuery = "getOffenders")
    public List<Offender> getOffenders() {
        throw new AssertionError();
    }
}
